package cn.dgut.changeManager;

import cn.dgut.Utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@WebServlet("/AddCourse")
public class AddCourseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        int teacher = Integer.parseInt(request.getParameter("teacher"));
        String hours = request.getParameter("hours");
        String type = request.getParameter("type");
        String begintime = request.getParameter("begintime");
        String pay = request.getParameter("pay");
        String info = request.getParameter("content");
        String isSell = request.getParameter("isSell");
        int status = 0;
        if(teacher!=0){
            String sql = "INSERT INTO `course` VALUES(null,"+teacher+",'"+name+"',"+hours+",'"+type+"','"+begintime+"',"+pay+",'"+info+"',"+isSell+")";

            JDBCUtils jdbcUtils = new JDBCUtils();
            status = jdbcUtils.querySQL(sql);
        }

        HttpSession session = request.getSession(true);
        session.setAttribute("isRight", status);
        response.sendRedirect("/backstage/main_page.jsp?page=3");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}